package cn.gl.org.service.impl;

import cn.gl.basic.service.impl.BaseServiceImpl;
import cn.gl.basic.util.Md5Utils;
import cn.gl.basic.util.StrUtils;
import cn.gl.org.domain.Employee;
import cn.gl.org.mapper.EmployeeMapper;
import cn.gl.org.service.IEmployeeService;
import cn.gl.usr.domain.Logininfo;
import cn.gl.usr.mapper.LogininfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 业务实现类：部门
 */
@Service
@Transactional
public class EmployeeServiceImpl extends BaseServiceImpl<Employee> implements  IEmployeeService {
    @Autowired
    private LogininfoMapper logininfoMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Override
    public void insert(Employee employee) {
        //处理盐值和密码
        String salt = StrUtils.getComplexRandomString(32);
        String md5Pwd = Md5Utils.encrypByMd5(employee.getPassword() + salt);
        employee.setSalt(salt);
        employee.setPassword(md5Pwd);
        //先添加logininfo
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(employee,logininfo);
        logininfo.setType(0);//管理员
        logininfo.setDisable(1);
        logininfoMapper.insert(logininfo);//获取到自增ID

        //添加user的数据
        employee.setLogininfo_id(logininfo.getId());
        super.insert(employee);
        //添加当前员工的角色信息
        if (employee.getRole_id()!=null) {
            employeeMapper.insertRole(employee);
        }
    }

    @Override
    public void delete(Long id) {
        //删除t_logininfo信息
        Employee employee = employeeMapper.laodOne(id);
        logininfoMapper.delete(employee.getLogininfo_id());

        //删除当前员工的角色信息:t_employee_role
        employeeMapper.deleteRoleByEmployeeId(id);
        //删除employee
        super.delete(id);
    }
    private Logininfo employee2Logininfo(Employee employee) {
        Logininfo logininfo = new Logininfo();
        //通过属性同名原则进行拷贝
        BeanUtils.copyProperties(employee,logininfo);
        logininfo.setType(0);
        logininfo.setDisable(1);
        return logininfo;
    }

    @Override
    public void update(Employee employee) {
        //处理盐值和密码
        String salt = StrUtils.getComplexRandomString(32);
        String md5Pwd = Md5Utils.encrypByMd5(employee.getPassword() + salt);
        employee.setSalt(salt);
        employee.setPassword(md5Pwd);


        //注意:修改的时候不应该修改密码 - 强制修改密码

        //创建Logininfo对象:拷贝了6个数据包括id[employee的id]
        Logininfo logininfo = employee2Logininfo(employee);

        Employee dbEmployee = employeeMapper.laodOne(employee.getId());
        //重新设置Logininfoid
        logininfo.setId(dbEmployee.getLogininfo_id());
        Logininfo dbLogininfo = logininfoMapper.laodOne(dbEmployee.getLogininfo_id());
        logininfo.setDisable(dbLogininfo.getDisable());

        //操作数据库
        logininfoMapper.update(logininfo);
        //employee - 前端传递的
        employee.setShop_id(dbEmployee.getShop_id());
        employee.setLogininfo_id(dbEmployee.getLogininfo_id());
        super.update(employee);

        //修改 = 先删再添加
        employeeMapper.deleteRoleByEmployeeId(employee.getId());
        if (employee.getRole_id()!=null) {
            employeeMapper.insertRole(employee);
        }
    }
}
